package com.zhang.crm.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Page;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.pojo.TUser;
import com.zhang.crm.pojo.req.UserReq;
import com.zhang.crm.result.CodeEnum;
import com.zhang.crm.result.R;
import com.zhang.crm.service.RedisService;
import com.zhang.crm.service.UserService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@RestController
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RedisService redisService;
    @GetMapping("test")
    public void setRedisKey() {
        System.out.println("测试成功");
    }
    @GetMapping("api/userInfo")
    public R getUSerInfo() {
        R r = R.OK(UserUtil.getUserInfo());
        return r;
    }

    @GetMapping("api/getUsers")
    public R getUsers(@RequestParam(value = "current", required = false) Integer current) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TUser> users = userService.getUsers(current);
        return R.OK(users);
    }

    @GetMapping("api/userDetail/{id}")
    public R getUserDetail(@PathVariable("id")Integer id) {
        TUser tUser = userService.getUserDetail(id);
        if (ObjectUtil.isEmpty(tUser.getCreatePo())) {
            tUser.setCreatePo(tUser.builder().build());
        }
        if (ObjectUtil.isEmpty(tUser.getEditPo())) {
            tUser.setEditPo(tUser.builder().build());
        }
        return tUser != null ? R.OK(tUser) : R.FAIL(CodeEnum.NOT_USER_DETAIL);
    }

    @PostMapping("api/addUser")
    public R addUser(TUser tUser) {
        Integer addNum = userService.addUser(tUser);
        return R.OK(addNum);
    }

    @PutMapping("api/editUser")
    public R editUser(UserReq userReq) {
        Boolean bool = userService.editUser(userReq);
        return bool ? R.OK() : R.FAIL("更新失败");
    }
     @DeleteMapping("api/delUser/{id}")
    public R userDelBatch(@PathVariable("id") Integer userId){
         Integer i = userService.delUserById(userId);
         return i > 0 ? R.OK() : R.FAIL("失败");
     }

    @DeleteMapping("api/userDelBatch")
    public R delUserBatch(@RequestParam(value = "ids") List<String> ids){
        Integer i = userService.delUserBatch(ids);
        return R.OK(i);
    }

    @GetMapping("api/loadUserSelect")
    public R getUserSelect() {
        List<TUser> nameList = userService.getUserSelectName();
        return R.OK(nameList);
    }

    @GetMapping("api/loadLoginInfo")
    public R getLoginInfo() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return R.OK(principal);
    }


}
